//
//  PannelViewController.m
//  Butler
//
//  Created by liaozhenming on 16/6/12.
//  Copyright © 2016年 liaozhenming. All rights reserved.
//

#import "PannelViewController.h"
#import "KKPageView.h"

#import "OrderViewController.h"
#import "KKCustomerEditView.h"
#import "KKGoodsEditView.h"
#import "KKEmployeeEditView.h"

@interface PannelViewController ()

@property (nonatomic, weak) IBOutlet KKPageView *pageView;
@property (nonatomic, strong) UIView *bottomView;

@property (nonatomic, strong) KKCustomerEditView *customerEditView;
@property (nonatomic, strong) KKGoodsEditView *goodsEditView;
@property (nonatomic, strong) KKEmployeeEditView *employeeEditView;

@property (nonatomic, strong) KKPageItemsList *customerPageItemsList;
@property (nonatomic, strong) KKPageItemsList *goodsPageItemsList;
@property (nonatomic, strong) KKPageItemsList *employeePageItemsList;

@end

@implementation PannelViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    _customerEditView = [[KKCustomerEditView alloc] init];
    _goodsEditView = [[KKGoodsEditView alloc] init];
    _employeeEditView = [[KKEmployeeEditView alloc] init];
    
    _goodsPageItemsList = [[KKPageItemsList alloc] initWithItems: [[KKButlerDataManager kk_manager] arr_allGoodsItems]];
    _customerPageItemsList = [[KKPageItemsList alloc] initWithItems: [[KKButlerDataManager kk_manager] arr_customerItems]];
    _employeePageItemsList = [[KKPageItemsList alloc] initWithItems: [[KKButlerDataManager kk_manager] arr_employeeItems]];
    
    [_pageView setPageViewCellType: 0 pageItemsList: _customerPageItemsList];
}

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear: animated];
    
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(notification_listenGoods:) name: KKButlerDataManagerUpdateNotificationNameGoods object: nil];
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(notification_listenEmployee:) name: KKButlerDataManagerUpdateNotificationNameEmployee object: nil];
    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(notification_listenCustomer:) name: KKButlerDataManagerUpdateNotificationNameCustomer object: nil];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear: animated];
    [[NSNotificationCenter defaultCenter] removeObserver: self];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Private methods

#pragma mark - Notification methods

- (void)notification_listenGoods:(NSNotification*)notification {
    
    [[KKButlerDataManager kk_manager] kk_searchAllGoodsItemsWithCategoryId: nil result:^(id object) {
        _goodsPageItemsList = [[KKPageItemsList alloc] initWithItems: object];
        [_pageView setPageViewCellType: 1  pageItemsList: _goodsPageItemsList];
    }];
}

- (void)notification_listenEmployee:(NSNotification*)notification {
    
    [[KKButlerDataManager kk_manager] kk_updateAllEmployeeItems:^(BOOL finished) {
        _employeePageItemsList = [[KKPageItemsList alloc] initWithItems: [[KKButlerDataManager kk_manager] arr_employeeItems]];
        [_pageView setPageViewCellType: 2  pageItemsList: _employeePageItemsList];
    }];
}

- (void)notification_listenCustomer:(NSNotification*)notification {
    
    [[KKButlerDataManager kk_manager] kk_updateAllCustomerItems:^(BOOL finished) {
        _customerPageItemsList = [[KKPageItemsList alloc] initWithItems: [[KKButlerDataManager kk_manager] arr_customerItems]];
        [_pageView setPageViewCellType: 0  pageItemsList: _customerPageItemsList];
    }];
}

#pragma mark - Action methods

- (IBAction)action_back:(id)sender {
    
    [self.navigationController popViewControllerAnimated: YES];
}

- (IBAction)action_order:(id)sender {
    
    switch (_segmentedControl.selectedSegmentIndex) {
        case 0:
        {
            [_customerEditView kk_show];
        }
            break;
        case 1:
        {
            [_goodsEditView kk_show];
        }
            break;
        case 2:
        {
            [_employeeEditView kk_show];
        }
            break;
    }
}

- (IBAction)action_segmented:(UISegmentedControl*)sender {
    
    switch (sender.selectedSegmentIndex) {
        case 0:
        {
            [_pageView setPageViewCellType: 0 pageItemsList: _customerPageItemsList];
        }
            break;
        case 1:
        {
            [_pageView setPageViewCellType: 1  pageItemsList: _goodsPageItemsList];
        }
            break;
        case 2:
        {
            [_pageView setPageViewCellType: 2  pageItemsList: _employeePageItemsList];
        }
            break;
    }
}

@end
